package lanou.autohome.findcar.fragments;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.ButterKnife;
import lanou.autohome.R;
import lanou.autohome.URLValues;
import lanou.autohome.basetool.BaseFragment;
import lanou.autohome.basetool.NetTool;
import lanou.autohome.collection.CollectionActivity;
import lanou.autohome.findcar.adapters.BrandAdapter;
import lanou.autohome.findcar.adapters.BrandLvRightAdapter;
import lanou.autohome.findcar.adapters.BrandHeaderDownSectionedAdapter;
import lanou.autohome.findcar.adapters.PinnedHeaderListView;
import lanou.autohome.findcar.beans.BrandBean;
import lanou.autohome.findcar.beans.BrandExpandableListviewBean;

/**
 * Created by dllo on 16/8/16.
 * 找车 -> 品牌
 */
public class BrandFragment extends BaseFragment implements View.OnClickListener {

    private RecyclerView recyclerView;
    private GridLayoutManager gridLayoutManager;
    private ListView rightlistView;
    private PinnedHeaderListView pinnedListView;
    private ArrayList<ArrayList<String>> nameAll;
    private BrandLvRightAdapter brandLvRightAdapter;
    private BrandHeaderDownSectionedAdapter sectionedAdapter;
    private BrandAdapter brandAdapter;
    private String[] order = {"A" ,"B" ,"C" ,"D" ,"F" ,"G" ,"H" ,"J" ,"K" ,"L" , "M" ,"N" ,"O" ,"P" ,"Q" ,"R" ,"S" ,"T" ,"W" ,"X" ,"Y" ,"Z" };
    private ArrayList<HashMap<String, String>> urlAll;

    @Override
    protected int setLayout() {
        return R.layout.fragment_findcar_brand;
    }

    @Override
    protected void initView() {
        pinnedListView = bindVie(R.id.pinnedListView);
        rightlistView = bindVie(R.id.listview_right_findcar);
        pinnedListView.addHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.fragment_findcar_brand_header, null));

        recyclerView = bindVie(R.id.recyclerview_brand_findcar);
        gridLayoutManager = new GridLayoutManager(getContext(), 5);

        //收藏按钮
        Button button = bindVie(R.id.btn_shoucang_findcar_brand);
        button.setOnClickListener(this);
    }

    @Override
    protected void intiData() {

        //右侧竖条listview , A , B , C...
        brandLvRightAdapter = new BrandLvRightAdapter(getContext());
        final ArrayList<String> titleStr = new ArrayList<>();
        titleStr.add("#");
        titleStr.add("A");
        titleStr.add("B");
        titleStr.add("C");
        titleStr.add("D");
        titleStr.add("F");
        titleStr.add("G");
        titleStr.add("H");
        titleStr.add("J");
        titleStr.add("K");
        titleStr.add("L");
        titleStr.add("M");
        titleStr.add("N");
        titleStr.add("O");
        titleStr.add("P");
        titleStr.add("Q");
        titleStr.add("R");
        titleStr.add("S");
        titleStr.add("T");
        titleStr.add("W");
        titleStr.add("X");
        titleStr.add("Y");
        titleStr.add("Z");
        brandLvRightAdapter.setTitleStr(titleStr);
        rightlistView.setAdapter(brandLvRightAdapter);
        rightlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mcontext, titleStr.get(position), Toast.LENGTH_SHORT).show();
            }
        });

        //左侧的listview
        mNetTool.getNetData(URLValues.ALL_BRAND_URL, BrandExpandableListviewBean.class, new NetTool.NetListener<BrandExpandableListviewBean>() {
            @Override
            public void onSuccess(BrandExpandableListviewBean bean) {
                ArrayList<String> name =  new ArrayList<String>();
                nameAll = new ArrayList<ArrayList<String>>();
                HashMap<String,String> url = new HashMap<String, String>();
                urlAll = new ArrayList<HashMap<String, String>>();
                for (int i = 0; i < order.length ; i++) {
                    for (int n = 0; n < bean.getResult().getBrandlist().get(i).getList().size(); n++) {
                        name.add(bean.getResult().getBrandlist().get(i).getList().get(n).getName());
                        url.put(bean.getResult().getBrandlist().get(i).getList().get(n).getName() ,
                                bean.getResult().getBrandlist().get(i).getList().get(n).getImgurl());
                    }
                    nameAll.add(name);
                    urlAll.add(url);

                    name = new ArrayList<String>();
                    url = new HashMap<String, String>();
                }
                sectionedAdapter = new BrandHeaderDownSectionedAdapter(mcontext);
                sectionedAdapter.setNameAll(nameAll);
                sectionedAdapter.setUrlAll(urlAll);
                pinnedListView.setAdapter(sectionedAdapter);
            }

            @Override
            public void onError(String errorMsg) {
                Log.d("BrandFragment", "网络有问题!!!!!!!!!!");
            }
        });

        //相应的item , 因为有头布局 , 所以需要判断
        rightlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int leftSection = 0;
                if(position < 2){
                    leftSection = position;
                }else {
                    for (int i = 0; i < position - 1; i++) {
                        leftSection += sectionedAdapter.getCountForSection(i) + 1;
                    }
                    leftSection ++;
                }
                pinnedListView.setSelection(leftSection);
            }
        });

        //头布局
        brandAdapter = new BrandAdapter(getContext());
        NetTool netTool = new NetTool();
        netTool.getNetData(URLValues.HOT_BRAND_URL, BrandBean.class, new NetTool.NetListener<BrandBean>() {
            @Override
            public void onSuccess(BrandBean brandBean) {
                brandAdapter.setBrandBeans(brandBean);
                recyclerView.setLayoutManager(gridLayoutManager);
                recyclerView.setAdapter(brandAdapter);
            }
            @Override
            public void onError(String errorMsg) {
            }
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.bind(getActivity());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_shoucang_findcar_brand:
                Intent shoucang = new Intent(getContext() , CollectionActivity.class);
                startActivity(shoucang);
            break;
        }
    }
}
